import { ConfigEnv, loadEnv, UserConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import pkg from './package.json';
import vueJsxPlugin from '@vitejs/plugin-vue-jsx';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import { resolve } from 'path';
import { formatToDateTime } from './src/utils/DateUtil';
import { viteMockServe } from 'vite-plugin-mock';
import { prismjsPlugin } from 'vite-plugin-prismjs'


const { dependencies, devDependencies, name, version } = pkg;

const __APP_INFO__ = {
  pkg: { dependencies, devDependencies, name, version },
  lastBuildTime: formatToDateTime(),
};
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const root = process.cwd();
  const { VITE_USE_MOCK } = loadEnv(mode, root);
  return {
    plugins: [
      vue(),
      vueJsxPlugin(),
      viteMockServe({
        localEnabled: VITE_USE_MOCK === 'true',
      }),
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: false, // css in js
          }),
        ],
      }),
      prismjsPlugin({
        languages: 'all', // 语言
        plugins: ['line-numbers','show-language','copy-to-clipboard','inline-color'],
        theme: 'okaidia',// 主题
        css: true,
      })
    ],
    base:"./",
    resolve: {
      alias: {
        '@': resolve(process.cwd(), '.', 'src'),
      },
    },
    server: {
      open: true, // 设置服务启动时是否自动打开浏览器
      cors: true, // 允许跨域
      // 设置代理，根据项目实际情况配置
      proxy: {
        '/api': {
          target: 'http://localhost:8888',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
    define: {
      __APP_INFO__: JSON.stringify(__APP_INFO__),
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          additionalData: `@import "src/styles/var.less";`,
        },
      },
    },
    build: {
      chunkSizeWarningLimit: 2000,
    },
  };
};
